package ru.ccfit.nsu.g7204.dronov.adPlatform;

import ru.ccfit.nsu.g7204.dronov.adPlatform.system.db.DBConnectionHolder;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Drongo
 * Date: 22.03.11
 * Time: 17:41
 * To change this template use File | Settings | File Templates.
 */
public class BannerDetailsDBManager {
    private PreparedStatement getDetailsStm;
    private PreparedStatement delFeatureStm;
    private PreparedStatement editFeatureStm;
    private PreparedStatement editBannerInfoStm;

    public BannerDetailsDBManager(DBConnectionHolder dbConnectionHolder) {
        Connection conn = dbConnectionHolder.getConnection();
        try {
            getDetailsStm =conn.prepareStatement("select f_id, f_info, f_val from complete_banners where id=?");
            editFeatureStm=conn.prepareStatement("call addFeatureVal2Banner(?,?,?)");//banner_id, feature_id, feature_value
            delFeatureStm=conn.prepareStatement("call delFeatureFromBanner(?,?)");//banner_id, feature_id
            editBannerInfoStm=conn.prepareStatement("update banners set info=? where id = ?");
        } catch (SQLException e) {
            ru.ccfit.nsu.g7204.dronov.adPlatform.utils.LoggingUtils.error(e.getLocalizedMessage(),e);
        }
    }

    public List<BannerFeatureInfo> getDetails(long id)
    {
        try {
            getDetailsStm.setLong(1, id);
            List<BannerFeatureInfo> features=new ArrayList<BannerFeatureInfo>();
            ResultSet rs= getDetailsStm.executeQuery();
            for (;rs.next();)
            {
                features.add(new BannerFeatureInfo(rs.getInt("f_id"),rs.getString("f_info"),rs.getDouble("f_val")));
            }
            return features;
        } catch (SQLException e) {
            ru.ccfit.nsu.g7204.dronov.adPlatform.utils.LoggingUtils.error(e.getLocalizedMessage(),e);
        }
        return null;
    }

    public void delFeature(long bannerId, int featureId)
    {
        try {
            delFeatureStm.setLong(1,bannerId);
            delFeatureStm.setInt(2,featureId);
            delFeatureStm.executeUpdate();
        } catch (SQLException e) {
            ru.ccfit.nsu.g7204.dronov.adPlatform.utils.LoggingUtils.error(e.getLocalizedMessage(),e);
        }
    }

    public void addOrEditFeature(long bannerId, int featureId, double newWeight)
    {
        try {
            editFeatureStm.setLong(1,bannerId);
            editFeatureStm.setInt(2,featureId);
            editFeatureStm.setDouble(3,newWeight);
            editFeatureStm.executeUpdate();
        } catch (SQLException e) {
            ru.ccfit.nsu.g7204.dronov.adPlatform.utils.LoggingUtils.error(e.getLocalizedMessage(),e);
        }

    }

    public void editBannerInfo(long bannerId, String newInfo)
    {
        try {
            editBannerInfoStm.setString(1,newInfo);
            editBannerInfoStm.setLong(2,bannerId);
            editBannerInfoStm.executeUpdate();
        } catch (SQLException e) {
            ru.ccfit.nsu.g7204.dronov.adPlatform.utils.LoggingUtils.error(e.getLocalizedMessage(),e);
        }
    }
}
